package com.qolome.component;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.qolome.dao.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author Qolome
 * @version V1.0
 * @Description 对象自处理
 * @date 2020年 01月12日 15:26:39
 */
@Component
@Slf4j
public class QolomeMetaObjectHandler implements MetaObjectHandler {
    private  final static String CREATE_ID = "creatorId";
    private  final static String CREATE_TIME = "createTime";
    private  final static String UPDATE_ID = "updatorId";
    private  final static String UPDATE_TIME = "updateTime";


    @Override
    public void insertFill (MetaObject metaObject) {
        autoFill (metaObject, CREATE_ID, CREATE_TIME);
        autoFill (metaObject, UPDATE_ID, UPDATE_TIME);
    }

    @Override
    public void updateFill (MetaObject metaObject) {


        autoFill (metaObject, UPDATE_ID, UPDATE_TIME);
    }

    private void autoFill (MetaObject metaObject, String id, String time) {
        if(
                metaObject.hasSetter (id)
                && Objects.isNull (getFieldValByName (id,metaObject))
        ){
            strictInsertFill(metaObject, id, Long.class, 1215521875639393333L);
        }
        if(
                metaObject.hasSetter (time)
                        && Objects.isNull (getFieldValByName (time,metaObject))

        ){
            strictInsertFill(metaObject, time, LocalDateTime.class,  LocalDateTime.now ());
        }
    }
}
